//route 访问路径，路由
//router 路由管理者
import { createRouter, createWebHashHistory } from 'vue-router';// 创建路由实例并传递 `routes` 配置const router = createRouter({// 内部提供了 history 模式的实现。为了简单起见，我们在这里使用 hash 模式。
//创建路由 route

import Home  from  "@/views/Home.vue"
import Login  from "@/views/Login.vue"
import User  from "@/views/users/User.vue"
import UserArticles  from "@/views/users/UserArticles.vue"
import UserArticleMain  from "@/views/users/UserArticleMain.vue"
import Register from "@/views/Register.vue"
import Retrieve from "@/views/Retrieve.vue"
import Articles from "@/views/Articles.vue"
import Mqtt from "@/views/mqtt.vue"
import {useUserToken} from "@/store/counter";
const routes = [
    { path: '/', component: Home ,name:"home"},
    { path: '/login', component: Login ,name:"login"},
    { path: '/user', component: User ,name:"user"},
    { path: '/userArticles', component: UserArticles ,name:"userArticles"},
    { path: '/userArticleMain/:id', component: UserArticleMain ,name:"userArticleMain"},
    { path: '/register', component: Register ,name:"register"},
    { path: '/retrieve', component: Retrieve ,name:"retrieve"},
    { path: '/mqtt', component: Mqtt ,name:"mqtt"},
    { path: '/articles', component: Articles ,name:"articles" },//携带参数
]
//创建路由管理 router
const router = createRouter({// 内部提供了 history 模式的实现。为了简单起见，我们在这里使用 hash 模式。
    history: createWebHashHistory(),
    routes,}
);
//白名单
const whitelist=['login','register','retrieve']
router.beforeEach((to,from,next)=>{
    const userToken=useUserToken()
    const token=userToken.user.token;
    if (token=='' && !whitelist.includes(to.name)){
        next({name:'login'})
    }else {
        next()
    }
})
//导出路由
export default router